<?php
/*
Plugin Name: koumei's bing! site search
Plugin URI: http://koumei.net/web-kbing
Description: This is a plug-in to integrate bing! site search.
Version: 1.0
Author: Koumei
Author URI: http://koumei.net
*/
?>
<?php
/**
This is a class for bing integration

*/

include_once('xtemplate.class.forbing.php');

class KBing{
	private static $GENERAL_OPTION_NAME = 'web_kbing_general';
	private static $WIDGET_ID = 'kbing';
	
	public function showWidget($args){
		$general_option = get_option(self::$GENERAL_OPTION_NAME);
		$site = preg_replace('/http:\/\//', '', get_bloginfo('wpurl'));
		$_widget_title = !empty($general_option['widget_title'])?$general_option['widget_title']:'bing!';	
		$_bing_search_site = !empty($general_option['bing_search_site'])?$general_option['bing_search_site']:$site;	
		
		echo $args['before_widget'];
		echo $args['before_title'] . $_widget_title . $args['after_title'];
		
		$widget_xtpl = new XTemplateForBing('widget.xtpl');
		
		
		
		$widget_xtpl->assign('SEARCH_SITE', $_bing_search_site);
		$widget_xtpl->parse('widget');
		
		$widget_xtpl->out('widget');
		
		echo $args['after_widget'];		
	}
	
	public function showControl(){
		
		$general_option = get_option(self::$GENERAL_OPTION_NAME);
		
		echo '<p><label>Widget Title: <input name="bing_widget_title" type="text" value="'.$general_option['widget_title'].'" /></label></p><p><label>Search Site: <input name="bing_search_site" type="text" value="'.$general_option['bing_search_site'].'" /></label></p>';
		
		if (isset($_POST['bing_widget_title'])){
			$general_option['widget_title'] = attribute_escape($_POST['bing_widget_title']);	
			$general_option['bing_search_site'] = attribute_escape($_POST['bing_search_site']);
			update_option(self::$GENERAL_OPTION_NAME, $general_option);
	
		}
	}	
	
	public function initialise(){
		register_sidebar_widget(self::$WIDGET_ID, array('KBing', 'showWidget'));
		register_widget_control(self::$WIDGET_ID, array('KBing', 'showControl'));		
	}
	
}

add_action('widgets_init', array('KBing', 'initialise'));


?>